package com.ias.agdis_r.view.dialogFragment;

import android.app.DialogFragment;
import android.os.Bundle;
//import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
//import android.support.v7.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
//import android.support.v7.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.action.task.TaskOprateAction;
import com.ias.agdis_r.adapter.SelectedWorkGroupMemberAdapter;
import com.ias.agdis_r.adapter.UnselectedWorkGroupMemberAdapter;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.task.vo.SubmitTaskProcessRequestVO;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.Member;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.WorkGroupLoadMembersResponseVO;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.view.OnRecyclerviewItemClickListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by xr.g on 2018/4/8.
 */

public class SelectWorkGroupMemberDialogFragment extends DialogFragment {
    private String tnb;
    private String confirm_action;
    private RecyclerView rvSelect, rvUnselect;
    private TextView tvCount;
    private LinearLayout llSelectAll, llClearAll, llCancel, llConfirm;
    private SelectedWorkGroupMemberAdapter selectAdapter;
    private UnselectedWorkGroupMemberAdapter unselectAdapter;
    private List<Member> allMember = new ArrayList<>();
    private List<SubmitTaskProcessRequestVO.TaskUser> taskUsers;
    private String countStr;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_select_work_group_member, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initView();
        refreshData();
    }

    private void initData() {
        tnb = getArguments().getString(MessageModel.TNB);
        confirm_action = getArguments().getString("confirm_action");
        BoxDBHelper boxDBHelper = new BoxDBHelper(getActivity());
        taskUsers = boxDBHelper.getTaskUsers(tnb);
        boxDBHelper.closeDB();
        countStr = getResources().getString(R.string.countString);
    }

    private void initView() {
        rvSelect = (RecyclerView) getView().findViewById(R.id.rv_selected);
        rvUnselect = (RecyclerView) getView().findViewById(R.id.rv_unselected);
        rvSelect.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvUnselect.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvSelect.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        rvUnselect.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        selectAdapter = new SelectedWorkGroupMemberAdapter(taskUsers);
        unselectAdapter = new UnselectedWorkGroupMemberAdapter(allMember);
        selectAdapter.setOnItemClickListener(new OnRecyclerviewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (position < taskUsers.size()) {
                    taskUsers.remove(position);
                    refreshData();
                }
            }
        });
        unselectAdapter.setOnItemClickListener(new OnRecyclerviewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (position < allMember.size()) {
                    Member member = allMember.get(position);
                    SubmitTaskProcessRequestVO.TaskUser taskUser = new SubmitTaskProcessRequestVO.TaskUser();
                    taskUser.setUsno(member.getMember_usno());
                    taskUser.setUnam(member.getMember_name());
                    taskUsers.add(taskUser);
                    refreshData();
                }
            }
        });
        rvSelect.setAdapter(selectAdapter);
        rvUnselect.setAdapter(unselectAdapter);
        llCancel = (LinearLayout) getView().findViewById(R.id.ll_cancel);
        llCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        llConfirm = (LinearLayout) getView().findViewById(R.id.ll_confirm);
        llConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTaskUsers();
                if (S.isNotEmpty(confirm_action)) {
                    TaskOprateAction.getInstance().excuteAction(
                            confirm_action, tnb, null);
                }
                dismiss();
            }
        });
        tvCount = (TextView) getView().findViewById(R.id.tv_count);
        llSelectAll = (LinearLayout) getView().findViewById(R.id.ll_selectAll);
        llSelectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Member member : allMember) {
                    SubmitTaskProcessRequestVO.TaskUser taskUser = new SubmitTaskProcessRequestVO.TaskUser();
                    taskUser.setUsno(member.getMember_usno());
                    taskUser.setUnam(member.getMember_name());
                    taskUsers.add(taskUser);
                }
                refreshData();
            }
        });
        llClearAll = (LinearLayout) getView().findViewById(R.id.ll_clearAll);
        llClearAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                taskUsers.clear();
                refreshData();
            }
        });
    }

    private void updateTaskUsers() {
        BoxDBHelper boxDBHelper = new BoxDBHelper(getActivity());
        boxDBHelper.saveTaskUsers(taskUsers, tnb);
        boxDBHelper.closeDB();
    }

    private void refreshData() {
        WorkGroupLoadMembersResponseVO vo = JavaBeanManager.getInstance().getWorkGroupLoadInfo();
        if (vo != null) {
            allMember.clear();
            allMember.addAll(vo.getMembers());
        }
        if (null != taskUsers) {
            Iterator<SubmitTaskProcessRequestVO.TaskUser> iteratorTaskUser = taskUsers.iterator();
            boolean taskUsersRemove = false;
            while (iteratorTaskUser.hasNext()) {
                boolean hasCheck = false;
                SubmitTaskProcessRequestVO.TaskUser user = iteratorTaskUser.next();
                Iterator<Member> iteratorMember = allMember.iterator();
                while (iteratorMember.hasNext()) {
                    if (user.getUsno().equals(iteratorMember.next().getMember_usno())) {
                        iteratorMember.remove();
                        hasCheck = true;
                        break;
                    }
                }
                if (!hasCheck) {
                    taskUsersRemove = true;
                    iteratorTaskUser.remove();
                }
            }
            if (taskUsersRemove) {
                updateTaskUsers();
            }
        }
        selectAdapter.notifyDataSetChanged();
        unselectAdapter.notifyDataSetChanged();
        tvCount.setText(String.format(countStr, taskUsers.size(), allMember.size() + taskUsers.size()));
    }

    public void receiveCDOF() {
        refreshData();
    }
}
